package com.aoasaa;

import java.io.IOException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class UserPlayingAudioFiles extends Activity
{
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	boolean isPLAYING;
	String notes_url;
	 MediaPlayer mp ; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playingaudiofile);
		
		 // accessing shared preference     
 		sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
 		editor_obj=sp_obj.edit();
 		
 		 notes_url=sp_obj.getString("retrivedurl", "NOA");
		// on play button
		
		Button btn_play=(Button)findViewById(R.id.button_audioplay);
		btn_play.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			startPlaying();	
			}
		});
		Button btn_stop=(Button)findViewById(R.id.button_audiostop);
		btn_stop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub 
				stopPlaying();
			}
		});
	}
	public void startPlaying() {
   
	    if (!isPLAYING) {              
	        isPLAYING = true;         
	       // mp = new MediaPlayer();   
	        try
	        {
	        	Uri uri = Uri.parse("http://10.0.2.2:88/archiveapplicationfiles/Rayman_2_music_sample.ogg");
//	         	mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
//	            mp.setDataSource(UserPlayingAudioFiles.this,uri); 
//	            mp.prepare();
	        	 MediaPlayer mp = MediaPlayer.create(getBaseContext(),uri);
	            mp.start();
	        } 
	        catch (Exception e) {   
	            Log.i("audio check", "prepare() failed");
	        }             
	    } else {
	        isPLAYING = false; 
	        stopPlaying();   
	    }
	}

	public void stopPlaying() {
	    mp.release();
	    mp = null;
	}
}
